package com.lxmes.gs.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.lxmes.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.ProjectStatisticsMapper;
import com.lxmes.gs.domain.ProjectStatistics;
import com.lxmes.gs.service.IProjectStatisticsService;

/**
 * 项目统计Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class ProjectStatisticsServiceImpl extends ServiceImpl<ProjectStatisticsMapper,ProjectStatistics> implements IProjectStatisticsService{
    
    @Autowired
    private ProjectStatisticsMapper projectStatisticsMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询项目统计
     * 
     * @param id 项目统计主键
     * @return 项目统计
     */
    @Override
    public ProjectStatistics selectProjectStatisticsById(Long id){
        return projectStatisticsMapper.selectById(id);
    }

    /**
     * 查询项目统计列表
     * 
     * @param projectStatistics 项目统计
     * @return 项目统计
     */
    @Override
    public List<ProjectStatistics> selectProjectStatisticsList(ProjectStatistics projectStatistics){
        return projectStatisticsMapper.selectList(Wrappers.lambdaQuery(projectStatistics));
    }

    /**
     * 新增项目统计
     * 
     * @param projectStatistics 项目统计
     * @return 结果
     */
    @Override
    public int insertProjectStatistics(ProjectStatistics projectStatistics){
        this.insertOrUpdatePre(projectStatistics);
        return projectStatisticsMapper.insert(projectStatistics);
    }

    private void insertOrUpdatePre(ProjectStatistics projectStatistics) {
        Double completedHours = ObjectUtil.defaultIfNull(projectStatistics.getCompletedHours(),0D);
        projectStatistics.setActualPersonnel(Convert.toLong(completedHours /8));
    }

    /**
     * 修改项目统计
     * 
     * @param projectStatistics 项目统计
     * @return 结果
     */
    @Override
    public int updateProjectStatistics(ProjectStatistics projectStatistics){
        this.insertOrUpdatePre(projectStatistics);
        return projectStatisticsMapper.updateById(projectStatistics);
    }

    /**
     * 批量删除项目统计
     * 
     * @param ids 需要删除的项目统计主键
     * @return 结果
     */
    @Override
    public int deleteProjectStatisticsByIds(Long[] ids){
        return projectStatisticsMapper.deleteProjectStatisticsByIds(ids);
    }

    /**
     * 删除项目统计信息
     * 
     * @param id 项目统计主键
     * @return 结果
     */
    @Override
    public int deleteProjectStatisticsById(Long id){
        return projectStatisticsMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<ProjectStatistics> projectStatisticsList, boolean updateSupport) {
        if (StringUtils.isNull(projectStatisticsList) || projectStatisticsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ProjectStatistics projectStatistics : projectStatisticsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, projectStatistics);
                this.insertProjectStatistics(projectStatistics);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

  
}
